/* eslint-disable no-alert */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import 'components/dash/main.css'
import { Switch, Redirect, Route } from 'react-router'
import { PrivateRoute } from 'components/route'
import RoutePaths from 'utils/constents/routePath'
import DataSet from 'containers/dataset'
import Users from 'containers/users'
import Project from 'containers/projects'
import Message from 'containers/message'
import Org from 'containers/org'
import { global as globalDuck } from 'ducks'
import Modal from 'components/confirmModal'
import Home from './homePage'
import Nav from './nav'

class PrivateHomeView extends Component {
  componentDidMount() {
    const { actionCheckReturnUrl } = this.props
    actionCheckReturnUrl()
  }

  render() {
    const { user } = this.props
    return (
      <Nav>
        <Switch>
          {user.plan === 'label' && (
            <Redirect exact path={RoutePaths.DASHBOARD} to={`${RoutePaths.HOME}`} />
          )}
          {user.plan !== 'label' && (
            <Redirect exact path={RoutePaths.DASHBOARD} to={`${RoutePaths.ORG}/${user.name}`} />
          )}
          <PrivateRoute path={RoutePaths.HOME} component={Home} exact />
          <PrivateRoute path={RoutePaths.DATASET} component={DataSet} exact={false} />
          <PrivateRoute path={RoutePaths.USERS} component={Users} exact={false} />
          <PrivateRoute path={RoutePaths.ORG} component={Org} exact={false} />
          <PrivateRoute path={RoutePaths.PROJECT} component={Project} exact={false} />
          <PrivateRoute path={RoutePaths.MESSAGE} component={Message} exact />
          <Route path="empty" component={null} key="empty" />
          <Redirect exact path="*" to={RoutePaths.DASHBOARD} />
        </Switch>
        <Modal />
      </Nav>
    )
  }
}

const mapStateToProps = state => ({
  user: globalDuck.selectors.user(state),
})

const mapDispatchToProps = {
  actionCheckReturnUrl: globalDuck.creators.actionCheckReturnUrl,
}

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(PrivateHomeView)
